package com.ttbj.controller.salesman;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.PermissionDto;
import com.ttbj.api.dto.RoleDto;
import com.ttbj.api.dto.SalesmanDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.salesman.PermissionService;
import com.ttbj.api.salesman.RolePermissionService;
import com.ttbj.api.salesman.RoleService;
import com.ttbj.constant.Const;
import com.ttbj.util.ViewDataUtil;


@Controller
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private RolePermissionService rolePermissionService;
	
	
	 /**
	  * 系统管理的系统角色管理页面
	  * @return
	  */
	 @RequestMapping("/role/sysrole_list.htm")
	 public String sysrole_list(HttpServletRequest request){
		 return "sysrole/sysrole_list";
	 }
	 
	 /**
	  * 添加角色页面
	  * @return
	  */
	 @RequestMapping("/role/updateUserRole.htm")
	 public String edit_sysrole(HttpServletRequest request){
		 return "sysrole/edit_sysrole";
	 }
	 
	 
	 /**
	  * 权限管理页面
	  * @return
	  */
	 @RequestMapping("/role/sysrole_permission.htm")
	 public String sysrole_permission(HttpServletRequest request){
		 return "sysrole/sysrole_permission";
	 }
	 
	/**
	 * 通过公司编号查询该公司下的所有角色
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/role/getRolesByCompanyNum.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getRolesByCompanyNum(HttpServletRequest request, HttpServletResponse response) {
		//当前登录用户
        SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
        List<RoleDto> roleDtoList = this.roleService.findRolesByComnum(salesmanDto);
		return ViewDataUtil.buildSuccessJson("通过公司编号查询角色成功", roleDtoList);
	}
	
	/**
	 * 分页查询角色
	 * @param request
	 * @param salesmanDto 登录用户信息
	 * @return
	 */
	@RequestMapping(value="/role/getRolesPage.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getRolesPage(HttpServletRequest request, Page page) {
		try {
			//获取登录用户
			SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
			//获取登录用户下的角色
			List<RoleDto> roleDtoList =  this.roleService.queryAllSalemanRoleByPage(page, salesmanDto);
			int count = 0;
			if (null != roleDtoList) {
				count = this.roleService.queryAllSalemanRoleByPageCount(salesmanDto);
			}
			return ViewDataUtil.buildSuccessPageJson("查询角色成功", roleDtoList, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 添加角色
	 * @param request
	 * @param roleDto 角色信息
	 * @return 
	 */
	@RequestMapping(value="/role/addToRoleInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String addToRoleInfo(HttpServletRequest request, RoleDto roleDto) {
		try {
			//获取登录用户
			SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
			this.roleService.addRole(roleDto, salesmanDto);
			return ViewDataUtil.buildSuccessJson("添加角色成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 根据角色id查询角色信息，用于简单的回显数据，
	 * 显示角色名称和角色描述。
	 * @param request 
	 * @param roleId 角色ID
	 * @return
	 */
	@RequestMapping(value="/role/getRoleInfoById.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getRoleInfoById(HttpServletRequest request, @RequestParam("id") Long roleId) {
		try {
			RoleDto roleDto = this.roleService.queryRoleById(roleId);
			return ViewDataUtil.buildSuccessJson("回显角色信息成功", roleDto);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 修改角色
	 * @param request
	 * @param roleDto 角色信息
	 * @return
	 */
	@RequestMapping(value="/role/modifyRoleInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String modifyRoleInfo(HttpServletRequest request, RoleDto roleDto) {
		try {
			this.roleService.updateRoleById(roleDto);
			return ViewDataUtil.buildSuccessJson("修改角色成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 删除角色，同时删除 角色-权限 关系
	 * @param request
	 * @param roleId 角色ID
	 * @return
	 */
	@RequestMapping(value="/role/delRoleInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String delRoleInfo(HttpServletRequest request, Long roleId) {
		try {
			this.roleService.deleteRoleById(roleId);
			return ViewDataUtil.buildSuccessJson("删除角色成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 根据角色ID查询权限
	 * @param request
	 * @param roleId 角色ID
	 * @return 权限list
	 */
	@RequestMapping(value="/role/getPermissionByRoleId.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getPermissionByRoleId(HttpServletRequest request, @RequestParam("roleId") long roleId) {
		List<PermissionDto> pDtoList =  this.permissionService.queryPermissionByRoleId(roleId);
		 return ViewDataUtil.buildSuccessJson("根据角色ID查询权限", pDtoList); 
	}
	
	/**
	 * 根据角色ID查询的权限列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/role/getAllPermission.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getAllPermission(HttpServletRequest request) {
		//获取登录用户
		SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
		List<PermissionDto> pList =  this.permissionService.queryAll(salesmanDto.getSalesmanRoleId());
		return ViewDataUtil.buildSuccessJson("可分配的权限", pList);
	}
	
	/**
	 * 更新角色-权限 关系
	 * @param request
	 * @param roleId 角色ID
	 * @param roles 
	 * @return
	 */
    @RequestMapping(value = "/role/updateRolePermission.htm", method=RequestMethod.POST, produces = {"text/html;charset=UTF-8;","application/json;"})
    @ResponseBody
    public String updateRolePermission(HttpServletRequest request,
                                       @RequestParam("roleId") long roleId,
                                       @RequestParam("roles[]") List<Long> roles) {
        try{
            if (!roles.isEmpty()){
                this.rolePermissionService.updateRolePermission(roles,roleId);
            }
            return ViewDataUtil.buildSuccessJson("角色权限更新成功");
        } catch (BusinessException be) {
            return ViewDataUtil.buildFailureJson(be.getErrorCode(), be.getMessage());
        }
    }
}
